package com.example.Adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.shopapp.R;
import com.example.viewmodel.Channel;

import java.util.List;

public class ChannelAdapter extends BaseAdapter {
    private Context ctx;
    private List<Channel> channel;

    public ChannelAdapter(Context ctx, List<Channel> channels) {
        this.ctx = ctx;
        this.channel = channels;
    }

    @Override
    public int getCount() {
        return this.channel.size();
    }

    @Override
    public Object getItem(int i) {
        return this.channel.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder;
        if (view == null){
            view = LayoutInflater.from(this.ctx).inflate(R.layout.home_channel_item, null);
            viewHolder = new ViewHolder();
            viewHolder.home_channel_img = view.findViewById(R.id.home_channel_img);
            viewHolder.home_channel_text = view.findViewById(R.id.home_channel_text);

            view.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder)view.getTag();
        }

        viewHolder.home_channel_text.setText(this.channel.get(i).getName());
        Glide.with(view.getContext())
                .load(this.channel.get(i).getIconUrl())//传入数据
                .into(viewHolder.home_channel_img);//传给组件

        return view;
    }

    static class ViewHolder{
        private ImageView home_channel_img;
        private TextView home_channel_text;
    }
}
